package com.ideal.gpt.app.docqajob.localjob;

import java.io.File;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;

import com.lzwork.utility.file.FileUtility;
import com.lzwork.utility.officeparser.ExcelLoader;
import com.lzwork.utility.officeparser.ExcelReader;
import com.lzwork.utility.officeparser.ExcelReader.Line;
import com.lzwork.utility.officeparser.ExcelReader.Page;
import com.lzwork.utility.officeparser.ExcelSaver;

/**
 * @author LiZheng
 * @date 2023年11月7日 上午9:27:18
 */

public class TypeSplit extends ExcelSaver
{
	final static String MAIN_TAB = "数据";
	final static String TYPE_COL = "问题类型";

	public static void main(String[] args)
	{
		File file = new File("v:/12345/exam/考试板块相关问题及答案20231106_split.xls");

		Workbook wb = ExcelLoader.loadWorkBook(file);

		Map<String, Sheet> sheetMap = new HashMap<>();
		Sheet source = null;

		for (Sheet sheet : wb)
		{
			String name = sheet.getSheetName();

			if (MAIN_TAB.equals(name))
				source = sheet;
			else
				sheetMap.put(name, sheet);
		}

		if (source == null)
		{
			System.out.println("Source sheet not found: " + MAIN_TAB);
			return;
		}

		Page p = ExcelReader.readSheet(0, source, 0, 0, false);

		for (Line line : p.getLines())
		{
			String type = line.getFieldData(TYPE_COL);

			Sheet target = sheetMap.get(type);
			if (target == null)
			{
				System.out.println("Unknown type [" + type + "] at line " + line.getIndex());
				continue;
			}

			appendLine(target, Arrays.asList(line.getStringData()));
		}
		
		File outFile = FileUtility.renameFileSafe(file, "_s", true);
		saveXlsFile(wb, outFile, true);
	}
}
